// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MailServiceImp.java

package com.iphaeton.lms.business.mail;

import com.iphaeton.common.helper.IUploadHelper;
import com.iphaeton.common.helper.Pagination;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.mail.Mail;
import com.iphaeton.lms.pojo.organize.User;
import com.iphaeton.persistence.IPersistenceService;
import com.iphaeton.persistence.spring.JdbcPService;
import java.text.DateFormat;
import java.util.*;
import org.apache.log4j.Logger;

// Referenced classes of package com.iphaeton.lms.business.mail:
//            IMailService

public class MailServiceImp extends PersistentServiceSupport
    implements IMailService
{

    public MailServiceImp()
    {
    }

    public IUploadHelper getUpload()
    {
        return upload;
    }

    public void setUpload(IUploadHelper upload)
    {
        this.upload = upload;
    }

    public Mail findMailById(String id)
        throws BusinessException
    {
        Mail mail = null;
        try
        {
            mail = (Mail)hbpservice.get(id, com.iphaeton.lms.pojo.mail.Mail.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636EID\u67E5\u627E\u90AE\u4EF6\u5931\u8D25!");
        }
        return mail;
    }

    public Mail createMail(Mail mail)
        throws BusinessException
    {
        try
        {
            mail = (Mail)hbpservice.store(mail);
            hbpservice.commit();
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u521B\u5EFA\u65B0\u90AE\u4EF6\u5931\u8D25!");
        }
        return mail;
    }

    public void sendMail(Mail m, User u)
        throws BusinessException
    {
        String sql = "insert into t_user_mail (mail_id,receive_id,receive_userId,receive_name,state)values('" + m.getId() + "','" + u.getId() + "','" + u.getUserId() + "','" + u.getName() + "','1') ";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u53D1\u9001\u90AE\u4EF6\u5931\u8D25!");
        }
    }

    public Pagination findSendedMail(String id, String row, Integer state, int pageSize)
        throws BusinessException
    {
        if(row == null || "".equals(row))
            row = "sendTime";
        String selectHql = "select count(m) from Mail m where m.sendId='" + id + "'";
        String queryHql = "from Mail m where  m.sendId='" + id + "'";
        if(state != null)
        {
            selectHql = selectHql + " and m.state='" + state + "'";
            queryHql = queryHql + "  and m.state='" + state + "'";
        }
        queryHql = queryHql + " order by m." + row;
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u53D1\u9001\u90AE\u4EF6\u8BB0\u5F55\u5931\u8D25!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(queryHql);
        return pageInfo;
    }

    public Pagination findReceivedMail(String id, Integer state, int pageSize)
        throws BusinessException
    {
        String selectSql = "select count(mail_id) as count from t_user_mail where receive_id='" + id + "'";
        if(state != null && state.intValue() != 0)
            selectSql = selectSql + " and state='" + state + "'";
        else
            selectSql = selectSql + " and state in (1,2)";
        List list = null;
        try
        {
            list = jdbcpservice.query(selectSql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u63A5\u6536\u90AE\u4EF6\u8BB0\u5F55\u6570\u91CF\u5931\u8D25!");
        }
        logger.debug(new Integer(list.size()));
        logger.debug(((Map)list.get(0)).get("count"));
        int itemCount = list.size() <= 0 ? 0 : ((Integer)((Map)list.get(0)).get("count")).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        return pageInfo;
    }

    public List findReceivedMail(String id, String row, Integer state, int pageSize, int currentPage)
        throws BusinessException
    {
        if(row == null || "".equals(row))
            row = "read_time";
        List list = null;
        String querySql1 = "select top " + pageSize + " * from t_user_mail um inner join t_mail m on um.receive_id='" + id + "' and m.id=um.mail_id ";
        String querySql2 = " and um.mail_id not in (select top " + pageSize * (currentPage - 1) + " um.mail_id from t_user_mail um inner join t_mail m on um.receive_id='" + id + "' and m.id=um.mail_id ";
        if(state != null && !state.equals(new Integer(0)))
        {
            querySql1 = querySql1 + " and um.state='" + state + "'";
            querySql2 = querySql2 + " and um.state='" + state + "')";
        } else
        {
            querySql1 = querySql1 + " and um.state in (1,2)";
            querySql2 = querySql2 + " and um.state in (1,2))";
        }
        querySql2 = querySql2 + " order by " + row;
        String querySql = querySql1 + querySql2;
        try
        {
            list = jdbcpservice.query(querySql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u63A5\u6536\u90AE\u4EF6\u5217\u8868\u5931\u8D25!");
        }
        return list;
    }

    public Pagination findRemovedMail(String id, int pageSize)
        throws BusinessException
    {
        String selectSql = "select count(id) as count from t_mail where send_id='" + id + "' and state=3 or id in (select mail_id from t_user_mail where receive_id='" + id + "' and state=3)";
        List list = null;
        try
        {
            list = jdbcpservice.query(selectSql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u5783\u573E\u7BB1\u90AE\u4EF6\u8BB0\u5F55\u6570\u91CF\u5931\u8D25!");
        }
        logger.debug(new Integer(list.size()));
        logger.debug(((Map)list.get(0)).get("count"));
        int itemCount = list.size() <= 0 ? 0 : ((Integer)((Map)list.get(0)).get("count")).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        return pageInfo;
    }

    public List findRemovedMail(String id, String row, int pageSize, int currentPage)
        throws BusinessException
    {
        List list = null;
        String querySql = "select top " + pageSize + " * from t_mail where send_id='" + id + "' and state=3 or id in (select mail_id from t_user_mail where receive_id='" + id + "' and state=3) and id not in (select top " + pageSize * (currentPage - 1) + " id from t_mail where send_id='" + id + "' and state=3 or id in (select mail_id from t_user_mail where receive_id='" + id + "' and state=3))";
        if(row == null || "".equals(row))
            row = "send_time";
        querySql = querySql + " order by " + row;
        try
        {
            list = jdbcpservice.query(querySql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5783\u573E\u7BB1\u6536\u90AE\u4EF6\u5217\u8868\u5931\u8D25!");
        }
        return list;
    }

    public void removeSendedMail(String id)
        throws BusinessException
    {
        Mail mail = findMailById(id);
        mail.setState(new Integer(3));
        createMail(mail);
    }

    public void removeReceivedMail(String mail_id, String user_id)
        throws BusinessException
    {
        String sql = "update t_user_mail set state=3 where receive_id='" + user_id + "' and mail_id='" + mail_id + "'";
        try
        {
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u5220\u9664\u63A5\u6536\u5230\u7684\u90AE\u4EF6\u5931\u8D25!");
        }
    }

    public List getMailDetail(String id)
        throws BusinessException
    {
        List list = null;
        String sql = "select um.receive_userId,um.receive_name,um.mail_id from t_user_mail um where um.mail_id='" + id + "'";
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u90AE\u4EF6\u8BE6\u7EC6\u5185\u5BB9\u5931\u8D25!");
        }
        return list;
    }

    public void deleteMail(String userId, String mailId)
        throws BusinessException
    {
        List list1 = null;
        List list2 = null;
        String sql1 = "select * from t_mail where send_id='" + userId + "' and id='" + mailId + "' and state!=4";
        String sql2 = "select * from t_user_mail where mail_id='" + mailId + "'";
        String sql3 = "";
        String sql4 = "";
        try
        {
            list1 = jdbcpservice.query(sql1);
            if(list1 != null && list1.size() == 1)
            {
                list2 = jdbcpservice.query(sql2);
                if(list2 != null && list2.size() > 0)
                {
                    sql3 = "update t_mail set state=4 where send_id='" + userId + "' and id='" + mailId + "'";
                    jdbcpservice.execute(sql3);
                } else
                {
                    sql3 = "delete from t_mail where send_id='" + userId + "' and id='" + mailId + "'";
                    String tag = findMailById(mailId).getTag();
                    if(tag != null && !"".equals(tag))
                        upload.remove(tag);
                    jdbcpservice.execute(sql3);
                }
            } else
            {
                sql3 = "delete from t_user_mail where receive_id='" + userId + "' and mail_id='" + mailId + "'";
                jdbcpservice.execute(sql3);
                list2 = jdbcpservice.query(sql2);
                if(list2 == null || list2.size() == 0)
                {
                    sql4 = "delete from t_mail where send_id='" + userId + "' and id='" + mailId + "' and state=4";
                    String tag = findMailById(mailId).getTag();
                    if(tag != null && !"".equals(tag))
                        upload.remove(tag);
                    jdbcpservice.execute(sql4);
                }
            }
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u7269\u7406\u5220\u9664\u90AE\u4EF6\u5931\u8D25!");
        }
    }

    public void clearMail(String userId)
        throws BusinessException
    {
        String sql1 = "delete from t_mail where send_id='" + userId + "' and state=3";
        String sql2 = "delete from t_user_mail where receive_id='" + userId + "' and state=3";
        try
        {
            jdbcpservice.execute(sql1);
            jdbcpservice.execute(sql2);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6E05\u7A7A\u5783\u573E\u7BB1\u5931\u8D25!");
        }
    }

    public void readedMark(String mailId, String userId)
        throws BusinessException
    {
        List list = null;
        Integer state = null;
        Date now = new Date();
        DateFormat df = DateFormat.getDateInstance();
        String current_time = df.format(now);
        String sql = "select state from t_user_mail where receive_id='" + userId + "' and mail_id='" + mailId + "'";
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u662F\u5426\u672A\u8BFB\u90AE\u4EF6\u5931\u8D25!");
        }
        state = (Integer)((Map)list.get(0)).get("state");
        if(state.equals(new Integer(1)))
        {
            String strSql = "update t_user_mail set state=2,read_time='" + current_time + "' where receive_id='" + userId + "' and mail_id='" + mailId + "'";
            try
            {
                jdbcpservice.execute(strSql);
            }
            catch(PersistenceException e)
            {
                e.printStackTrace();
                throw new BusinessException("\u8BBE\u7F6E\u90AE\u4EF6\u4E3A\u5DF2\u8BFB\u90AE\u4EF6\u5931\u8D25!");
            }
        }
    }

    public Integer mailCount(String id, String state)
        throws BusinessException
    {
        String sql = "select count(mail_id) as count from t_user_mail where receive_id='" + id + "'";
        if(state != "0")
            sql = sql + " and state='" + state + "'";
        else
            sql = sql + "and state=1 or state=2";
        List list = null;
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u90AE\u4EF6\u6570\u91CF\u65F6\u51FA\u9519!");
        }
        Integer i = list == null || list.size() <= 0 ? new Integer(0) : (Integer)((Map)list.get(0)).get("count");
        i = (Integer)((Map)list.get(0)).get("count");
        return i;
    }

    public List getSpecifiedMail(Pagination pageInfo)
        throws BusinessException
    {
        return getSpecifiedItems(pageInfo);
    }

    private static Logger logger;
    protected IUploadHelper upload;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.mail.MailServiceImp.class.getName());
    }
}
